<?php
class MesasController extends AppController
{
    public $uses = array('Mesa','Ambiente');
    public $layout = 'pizza';

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('*');
    }
    
    public function index()
    {
        $mesas = $this->Mesa->find('all');
        $this->set(compact('mesas'));
    }
    
    public function insertar()
    {
        if(!empty ($this->data))
        {
            $this->Mesa->create();
                if($this->Mesa->save($this->data))
                {
                    $this->Session->setFlash('Mesa Registrada con Exito...!!!');
                    $this->redirect(array('action'=>'index'));
                }
                else {
                    $this->Session->setFlash('No se puedo registrar la Mesa');
                }
        }
        $dambiente = $this->Ambiente->find('list', array('fields'=>'Ambiente.nombre'));
        $this->set(compact('dambiente'));
       // debug($dambiente);exit;
    }
    function editar($id = null)
    {
        $this->Mesa->id = $id;
        if (!$id) {
            $this->Session->setFlash('No Existe el tipo de Mesa');
            $this->redirect(array('action' => 'index'));
        }
        if (empty($this->data)) {
            $this->data = $this->Mesa->read();
            }
        else{
            if ($this->Mesa->save($this->data)) {
                $this->Session->setFlash('Se Guardo Correctamente la Mesa');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error al Guardar la Mesa');
            }
        }
        $dambiente = $this->Ambiente->find('list', array('fields'=>'Ambiente.nombre'));
        $this->set(compact('dambiente'));
       // debug($dambiente);exit;
    }
    function eliminar($id=null){
        $this->Mesa->id=$id;
        $this->data=$this->Mesa->read();
        if(!$id){
            $this->Session->setFlash('No existe la Mesa a eliminar');
            $this->redirect(array('action' =>'index'));
        }
        else
        {
            if($this->Mesa->delete($id)){
                $this->Session->setFlash('Se elimino la Mesa '.$this->data['Mesa']['nombre']);
                $this->redirect(array('action' =>'index'));
            }
            else{
                $this->Session->setFlash('Error al eliminar');
            }
        }
    }
}

?>
